/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.TipoMaterial;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import persistence.TipoMaterialJpaController;

/**
 *
 * @author Felipe Duarte
 */
public class TipoMaterialConverter implements Converter {

    TipoMaterialJpaController tipoMaterialJpaController;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.equals("")) {
            return null;
        }
        if (tipoMaterialJpaController == null) {
            tipoMaterialJpaController = (TipoMaterialJpaController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "tipoMaterialJpaController");
        }
        return tipoMaterialJpaController.findTipoMaterial(Integer.parseInt(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.equals("")) {
            return "";
        }
        TipoMaterial tipoMaterial = (TipoMaterial) value;
        return "" + tipoMaterial.getIdTipoMaterial();
    }
}